<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="./assets/news.css">
    <title>Document</title>
</head>

<body>
    <!-- 新闻列表 -->
    <div id="news-list">



        <!-- 一个新闻 -->
        <div class="news-item">
            <img class="thumb" src="" alt="" />
            <div class="right-box">
                <h1 class="title">5G商用在即，三大运营商营收持续下降</h1>
                <div class="footer">
                    <div>
                        <span>胡润百富</span>&nbsp;&nbsp;
                        <span>2019-10-28 10:14:38</span>
                    </div>
                    <span>评论数：66</span>
                </div>
            </div>
        </div>
    </div>

    <!-- 导入 jQuery 和 axios -->
    <script src="../lib/axios.js"></script>
    <script src="../lib/jquery.js"></script>
    <script>
        axios({
            url: "http://www.liulongbin.top:3009/api/news",
            method: "GET",
        }).then(({
            data: res
        }) => {
            console.log(res.data);
            // 清空父级
            $('#news-list').html("");
            // res.data 这个就是数组，直接遍历
            // 2 渲染到页面中 遍历数组，动态创建
            res.data.forEach(item => {
                let one = $(`<div class="news-item">
                        <img class="thumb" src="http://www.liulongbin.top:3009${item.img}" alt="" />
                        <div class="right-box">
                          <h1 class="title">${item.title}</h1>
                          <div class="footer">
                            <div>
                              <span>${item.source}</span>&nbsp;&nbsp;
                              <span>${item.time}</span>
                            </div>
                            <span>评论数：${item.cmtcount}</span>
                          </div>
                        </div>
                      </div>`);

                // 添加元素:
                $("#news-list").append(one);



            });
        })
    </script>

</body>

</html>